package com.ccl.keyassistant.adapter;
/*
 *项目名     keyassistant
 *包名       com.ccl.keyassistant.adapter
 *文件名     LocalRecycleAdapter
 *创建者     ccl
 *创建时间   2019-03-08 20:00
 *注解       主机界面数据适配器
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccl.keyassistant.R;
import com.ccl.keyassistant.entity.LocalFile;
import com.ccl.keyassistant.utils.L;


import java.util.List;

public class LocalRecycleAdapter extends RecyclerView.Adapter {

    private List<LocalFile> mDatas;
    private Context mContext;
    private final int TYPE_FILE = 0;
    private final int TYPE_FOLD = 1;
    private ItemClickListener mItemClickListener ;

    public LocalRecycleAdapter(Context context, List<LocalFile> data) {
        this.mContext = context;
        this.mDatas = data;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //准备视图，传递给ViewHolder
        switch (viewType) {
            case TYPE_FILE:
                View file = View.inflate(mContext, R.layout.local_file_item, null);
                FileHolder fileHolder = new FileHolder(file);
                return fileHolder;
            case TYPE_FOLD:
                View fold = View.inflate(mContext, R.layout.local_fold_item, null);
                FoldHolder foldHolder = new FoldHolder(fold);
                return foldHolder;
            default:
                break;
        }
        return null;

    }

    //进行viewholder中布局对象信息的设置
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int current_type = holder.getItemViewType();
        switch (current_type) {
            case TYPE_FILE:
                String fileName = mDatas.get(position).getFileName();
                String fileSize = mDatas.get(position).getFileSize();
                final FileHolder fileHolder = (LocalRecycleAdapter.FileHolder)holder;
                fileHolder.mFileName.setText(fileName);
                fileHolder.mFileSize.setText(fileSize);
                if(mItemClickListener!=null){
                    fileHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            boolean enabled = fileHolder.mFileState.isSelected();
                            fileHolder.mFileState.setSelected(!enabled);
                            mItemClickListener.onItemClick(position);
                        }
                    });
                }
                break;
            case TYPE_FOLD:
                String foldName = mDatas.get(position).getFileName();
                final FoldHolder foldHolder = (LocalRecycleAdapter.FoldHolder)holder;
                foldHolder.mFileName.setText(foldName);
                if(mItemClickListener!=null){
                    foldHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            L.i(position+"1");
                            mItemClickListener.onItemClick(position);

                        }
                    });
                }
                break;
            default:
                break;
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (mDatas.get(position).isDirectory()) {
            return TYPE_FOLD;
        }
        return TYPE_FILE;
    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }


    //实现点击事件
    public interface ItemClickListener{
         void onItemClick(int position) ;
    }
    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.mItemClickListener = itemClickListener ;
    }




    class FoldHolder extends RecyclerView.ViewHolder {

        private TextView mFileName;

        //进行view对象的初始化
        public FoldHolder(View itemView) {
            super(itemView);
            mFileName = itemView.findViewById(R.id.tv_fold_name);
        }

    }


    class FileHolder extends RecyclerView.ViewHolder {

        private TextView mFileName;
        private TextView mFileSize;
        private ImageView mFileState;

        //进行view对象的初始化
        public FileHolder(View itemView) {
            super(itemView);
            mFileName = itemView.findViewById(R.id.tv_file_name);
            mFileSize = itemView.findViewById(R.id.tv_file_size);
            mFileState = itemView.findViewById(R.id.iv_file_state);
        }


    }

}


